package lib;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;

import dao.WebsiteDAO;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import pojo.Website;

public class InteractiveForm extends JPanel{
	public static final String[] columnNames = {
		"WebsiteID", "Website", "Menu1", "Menu2", "Page", "WebContent", "Image", "BookContent", "BookInfo", "WebType"
	};
	
	protected JTable table;
	protected JScrollPane scroller;
	protected InteractiveTableModel tableModel;
	protected JButton apply;
	protected JButton cancel;
	protected List<Website> editedWebsites;
	protected List<Integer> rowIndex;
	
	public InteractiveForm(){
		editedWebsites = new ArrayList<Website>();
		rowIndex = new ArrayList<Integer>();
		initComponent();
	}
	
	public void initComponent(){
		tableModel = new InteractiveTableModel(columnNames);
		tableModel.addTableModelListener(new InteractiveForm.InteractiveTableModelListener());
		table = new JTable();
		table.setModel(tableModel);
		table.setSurrendersFocusOnKeystroke(true);
		
		tableModel.addEmptyRow();
		
		scroller = new JScrollPane(table);
		//table.setPreferredScrollableViewportSize(new java.awt.Dimension(500, 300));
		//setLayout(new BorderLayout());
        add(scroller, BorderLayout.CENTER);
		
		apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				WebsiteDAO websitedao = new WebsiteDAO();
				if(apply.isEnabled()){
					apply.setEnabled(false);
				}
				if(cancel.isEnabled()){
					cancel.setEnabled(false);
				}
				//for(int i = 0; i < editedWebsites.size(); i++){
				//	System.out.println(editedWebsites.get(i).getWebsiteId());
				//}
				for(int i = 0; i < editedWebsites.size(); i++){
					websitedao.UpdateOrInsert(editedWebsites.get(i));
				}
				editedWebsites.clear();
			}
		});
		
		cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(apply.isEnabled()){
					apply.setEnabled(false);
					
				}
				if(cancel.isEnabled()){
					cancel.setEnabled(false);
				}
				
			}
		});
		add(apply);
		add(cancel);
		//apply.setVisible(true);
		
		apply.setEnabled(false);
		cancel.setEnabled(false);
		
	}
	class InteractiveRenderer extends DefaultTableCellRenderer{
		
	}
	public class InteractiveTableModelListener implements TableModelListener{

		@Override
		public void tableChanged(TableModelEvent e) {
			// TODO Auto-generated method stub
			if(!apply.isEnabled() && !cancel.isEnabled()){
				apply.setEnabled(true);
				cancel.setEnabled(true);
			}
			boolean isOld = false;
			if(e.getLastRow() == tableModel.getRowCount() - 1){
				tableModel.addEmptyRow();
			}
			if(!rowIndex.isEmpty()){
				for(int i = 0; i < rowIndex.size(); i++){
					if(e.getLastRow() == rowIndex.get(i)){
						editedWebsites.set(i, (Website)tableModel.getValueAt(rowIndex.get(i), -1));
						isOld = true;
						break;
					}
				}
			}
			if(!isOld){
				editedWebsites.add((Website)tableModel.getValueAt(e.getLastRow(), -1));
				rowIndex.add(e.getLastRow());
				
			}
			
			
		}
		
		
	}
	
}
